using System.Collections.Generic;

namespace lab04
{
    interface IVector
    {
        void SetValue(int index, int value);
        int GetValue(int index);
        int GetNumRows();
    }

    class DenseVector : IVector
    {
        private readonly List<int> _values;

        public DenseVector(int numRows)
        {
            _values = new List<int>(numRows);
            for (int i = 0; i < numRows; ++i)
            {
                _values.Add(0);
            }
        }

        #region IVector Members

        public void SetValue(int index, int value)
        {
            _values[index] = value;
        }

        public int GetValue(int index)
        {
            return _values[index];
        }

        public int GetNumRows()
        {
            return _values.Count;
        }

        #endregion

     
    }

    class SparseVector : IVector
    {
        private readonly List<int> _indexes;
        private readonly int _numRows;
        private readonly List<int> _values;

        public SparseVector(int size)
        {
            _indexes = new List<int>();
            _values = new List<int>();
            _numRows = size;
        }

        #region IVector Members

        public int GetNumRows()
        {
            return _numRows;
        }

        public void SetValue(int index, int value)
        {
            if (value == 0) return;

            int position = IsExistsIndex(index);

            if (position == -1)
            {
                _indexes.Add(index);
                _values.Add(value);
            }
            else
            {
                _values[position] = value;
            }
        }

        public int GetValue(int index)
        {
            int position = IsExistsIndex(index);
            return position == -1 ? 0 : _values[position];
        }

        #endregion

        private int IsExistsIndex(int index)
        {
            int position = -1;
            for (int i = 0; i < _indexes.Count && (position == -1); i++)
            {
                if (_indexes[i] == index) position = i;
            }
            return position;
        }
    }
}